<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="sy.dao.mapper.${className}Mapper" > 

 	<!-- 	${tableName}  ${tableComment}  ${className}  -->
	
	<resultMap id="Base_Result_Map" type="sy.model.entity.${className}" >
	<#list columnList as columnEntity>
		<result property="${columnEntity.javaname}" column="${columnEntity.columnName}" />
	</#list>
	</resultMap>
	
	
	<!-- 查询列名 -->
	<sql id="Base_Columns" >
		<#list columnList as columnEntity>t.${columnEntity.columnName}<#if columnEntity_has_next> ,</#if></#list>
	</sql>
	   
	<!-- 表名-->
	<sql id="Base_Table_Name">
		${tableName} t
	</sql>
	 
	 <!-- 根据id查询 , 返回对象 -->
	<select id="queryById"  parameterType="Object"  resultType="sy.model.entity.${className}">
		select <include refid="Base_Columns" />
		from   <include refid="Base_Table_Name" />
		where t.id = ${r"#{id}"}
	</select>
	
	<!-- 分页条件查询 , 返回List(${className}) -->
	<select id="queryList" parameterType="Object" resultType="sy.model.entity.${className}" >
		select <include refid="Base_Columns" />
		from  <include refid="Base_Table_Name" />
		<include refid="Base_Where_Clause" />
	</select>
	
	<!-- 普通条件查询 , 返回List(Map) -->
	<select id="queryMapList" parameterType="Object" resultType="Map" >
		select <include refid="Base_Columns" />
		from  <include refid="Base_Table_Name" />
		<include refid="Base_Where_Clause" />
	</select>
	
	<!-- 新增 -->
	<insert id="add" parameterType="Object" useGeneratedKeys="true" keyProperty="id" >
		insert into	${tableName} 
			(<#list columnList as columnEntity><#if columnEntity.pki==false >${columnEntity.columnName}<#if columnEntity_has_next> ,</#if></#if></#list>)
		values 
			( <#list columnList as columnEntity><#if columnEntity.pki==false  >${r"#{"}${columnEntity.javaname}${r"}"}<#if columnEntity_has_next> ,</#if></#if></#list>  )
	</insert>
	
	<!-- 更新  -->
	<update id="update" parameterType="Object">
		update <include refid="Base_Table_Name" />
		set <#list columnList as columnEntity><#if columnEntity.pki==false  >${columnEntity.columnName}= ${r"#{"}${columnEntity.javaname}${r"}"}<#if columnEntity_has_next> ,</#if></#if></#list>
		where id=${r"#{id}"}
	</update>
	
	
	<!-- 更新记录，只修改不为空的字段 -->
	<update id="updateBySelective" parameterType="Object">
		update <include refid="Base_Table_Name" />
		set 
		<trim suffixOverrides=",">
		<#list columnList as columnEntity>
		<#if columnEntity.pki==false  >
			<if test="${columnEntity.javaname} != null<#if columnEntity.javaDataType=='String' > and ${columnEntity.javaname} != ''  </#if>">
				${columnEntity.columnName}= ${r"#{"}${columnEntity.javaname}${r"}"},
			</if>
		</#if>
		</#list>
		</trim>
		where id=${r"#{id}"}
	</update>
	
	<!-- 删除 -->
	<delete id="delete" parameterType="Object">
		delete from ${tableName} where id = ${r"#{id}"}
	</delete>

	
	<!-- 查询条件 -->
	<sql id="Base_Where_Clause">
		where 1=1
		<trim  suffixOverrides="," >
			<#list columnList as columnEntity>
			<if test="${columnEntity.javaname}!=null<#if columnEntity.javaDataType=='String' > and ${columnEntity.javaname}!=''</#if> " >
				<#if columnEntity.javaDataType=='String' >
				and t.${columnEntity.columnName} like concat(concat('%', ${r"#{"}${columnEntity.javaname}${r"}"}), '%')
				<#elseif columnEntity.javaDataType!='String' > and t.${columnEntity.columnName} = ${r"#{"}${columnEntity.javaname}${r"}"}
				</#if>
			</if>
			</#list>
		</trim>
	</sql>
	

</mapper>   
